home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter08 / RotateMoveZoomPlusPanel.java < prev    next >
Text File  |  2000-09-08  |  3KB  |  140 lines

  1.  
  2.  
  3.  
  4. package applets;
  5.  
  6. import shout3d.*;
  7. import shout3d.core.*;
  8. import shout3d.math.*;
  9.  
  10.  
  11. public class RotateMoveZoomPlusPanel extends Shout3DPanel implements DeviceObserver{
  12.     
  13.     Viewpoint camera;
  14.     float fov;
  15.  
  16.     int pixelStartX;
  17.     int pixelStartY;
  18.     int pixelEndX;
  19.     int pixelEndY;
  20.  
  21.     float[] worldPos = new float[3];
  22.  
  23.     float [] eulers = new float [3];
  24.     float [] axisAngle = new float [4];
  25.     Quaternion q = new Quaternion();
  26.  
  27.     float rotationFactor = 30.0f;  // pixels per radian
  28.     float translationFactor = .5f; //pixels per meter
  29.     float zoomFactor = 150.0f;  //pixels per meter
  30.  
  31.  
  32.     public RotateMoveZoomPlusPanel (Shout3DApplet applet){
  33.         super(applet);
  34.     }
  35.     
  36.         public void customInitialize() {
  37.  
  38.         addDeviceObserver(this,"MouseInput", null);
  39.  
  40.         camera = (Viewpoint) getCurrentBindableNode("Viewpoint");
  41.  
  42.         worldPos = camera.position.getValue();
  43.  
  44.         axisAngle = camera.orientation.getValue();
  45.         q.setAxisAngle(axisAngle);
  46.         q.getEulers(eulers);
  47.  
  48.         fov = camera.fieldOfView.getValue();
  49.  
  50.         String rotationFactorString = applet.getParameter("rotationFactor");
  51.         if (rotationFactorString != null){
  52.             rotationFactor = Float.valueOf(rotationFactorString).floatValue();
  53.         }
  54.  
  55.         String translationFactorString = applet.getParameter("translationFactor");
  56.         if (translationFactorString != null){
  57.             translationFactor = Float.valueOf(translationFactorString).floatValue();
  58.         }
  59.  
  60.         String zoomFactorString = applet.getParameter("zoomFactor");
  61.         if (zoomFactorString != null){
  62.             zoomFactor = Float.valueOf(zoomFactorString).floatValue();
  63.         }
  64.     }
  65.  
  66.  
  67.  
  68.     protected void finalize()  {
  69.  
  70.         removeDeviceObserver(this,"MouseInput");
  71.  
  72.     }
  73.  
  74.  
  75.     public boolean onDeviceInput(DeviceInput di, Object userData) {
  76.  
  77.         MouseInput mi = (MouseInput) di;
  78.  
  79.         switch (mi.which){
  80.             case MouseInput.DOWN:
  81.                 pixelStartX = mi.x;
  82.                 pixelStartY = mi.y;
  83.                 return true;                                        
  84.  
  85.  
  86.             case MouseInput.DRAG:
  87.  
  88.               //if left button used
  89.               if (mi.button == 0) {
  90.  
  91.                 pixelEndX = mi.x;
  92.                 pixelEndY = mi.y;
  93.                 int dragDistanceX = pixelEndX - pixelStartX;
  94.                 int dragDistanceY = pixelEndY - pixelStartY;
  95.                 pixelStartX = pixelEndX;
  96.                 pixelStartY = pixelEndY;
  97.                 
  98.  
  99.                 //ROTATION
  100.  
  101.                 float rotationDelta = -(dragDistanceX/rotationFactor);
  102.                 eulers[0] = eulers[0] + rotationDelta;
  103.                 q.setEulers(eulers);
  104.                 q.getAxisAngle(axisAngle);
  105.                 camera.orientation.setValue(axisAngle);
  106.  
  107.                 //TRANSLATION
  108.  
  109.                 float translationDelta = dragDistanceY/translationFactor;
  110.                 float[] vector = {0,0, translationDelta};
  111.                 q.xform(vector);
  112.                 worldPos[0] = worldPos[0] + vector[0];
  113.                 worldPos[2] = worldPos[2] + vector[2];
  114.                 camera.position.setValue(worldPos);
  115.     
  116.                 return true;
  117.  
  118.               }//end of 0 if
  119.  
  120.  
  121.               //if right button used
  122.               if (mi.button == 1)  {
  123.  
  124.                 pixelEndY = mi.y;
  125.                 int dragDistanceY = pixelEndY - pixelStartY;
  126.                 float fovDelta = dragDistanceY/zoomFactor;
  127.                 fov = fov + fovDelta;
  128.                 camera.fieldOfView.setValue(fov);
  129.  
  130.                 pixelStartY = pixelEndY;
  131.                 return true;
  132.  
  133.               }//end of 1 if
  134.  
  135.         }//end of switch
  136.  
  137.         return false;        
  138.     }
  139.  
  140. } //end of class